package com.jyw.business.ast.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.jyw.business.ast.domain.AstBorrow;
import com.jyw.business.ast.domain.AstConsumableRecord;
import com.jyw.business.ast.domain.dto.web.AstConsumableClaimDto;
import com.jyw.business.ast.domain.dto.web.AstConsumableClaimPageDto;
import com.jyw.business.ast.domain.dto.web.AstRejectedDto;
import com.jyw.business.ast.domain.vo.web.AstConsumableClaimDetailVo;
import com.jyw.business.ast.domain.vo.web.AstConsumableClaimPageVo;
import com.jyw.business.ast.domain.vo.web.AstConsumableClaimReserveVo;
import com.jyw.business.ast.domain.vo.web.AstConsumableClaimVo;
import com.jyw.business.ast.service.IAstConsumableRecordService;
import com.jyw.business.ast.service.IAstConsumableService;
import com.jyw.common.exception.ServiceException;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.system.utils.ConversionUtil;
import org.springframework.stereotype.Service;
import com.jyw.business.ast.mapper.AstConsumableClaimMapper;
import com.jyw.business.ast.domain.AstConsumableClaim;
import com.jyw.business.ast.service.IAstConsumableClaimService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 资产-耗材-耗材申领Service业务层处理
 *
 * @author system
 * @date 2023-10-24
 */
@Service
public class AstConsumableClaimServiceImpl extends ServiceImpl<AstConsumableClaimMapper, AstConsumableClaim> implements IAstConsumableClaimService {
    @Resource
    private AstConsumableClaimMapper astConsumableClaimMapper;

    @Resource
    private IAstConsumableRecordService recordService;

    @Resource
    private IAstConsumableService consumableService;

    /**
     * 查询资产-耗材-耗材申领
     *
     * @param id 资产-耗材-耗材申领主键
     * @return 资产-耗材-耗材申领
     */
    @Override
    public AstConsumableClaim selectAstConsumableClaimById(Long id) {
        return astConsumableClaimMapper.selectAstConsumableClaimById(id);
    }

    /**
     * 查询资产-耗材-耗材申领列表
     *
     * @param astConsumableClaim 资产-耗材-耗材申领
     * @return 资产-耗材-耗材申领
     */
    @Override
    public List<AstConsumableClaim> selectAstConsumableClaimList(AstConsumableClaim astConsumableClaim) {
        return astConsumableClaimMapper.selectAstConsumableClaimList(astConsumableClaim);
    }

    /**
     * 新增资产-耗材-耗材申领
     *
     * @param astConsumableClaim 资产-耗材-耗材申领
     * @return 结果
     */
    @Override
    public int insertAstConsumableClaim(AstConsumableClaim astConsumableClaim) {
        astConsumableClaim.setCreateTime(DateUtils.getNowDate());
        return astConsumableClaimMapper.insertAstConsumableClaim(astConsumableClaim);
    }

    /**
     * 修改资产-耗材-耗材申领
     *
     * @param astConsumableClaim 资产-耗材-耗材申领
     * @return 结果
     */
    @Override
    public int updateAstConsumableClaim(AstConsumableClaim astConsumableClaim) {
        return astConsumableClaimMapper.updateAstConsumableClaim(astConsumableClaim);
    }

    /**
     * 批量删除资产-耗材-耗材申领
     *
     * @param ids 需要删除的资产-耗材-耗材申领主键
     * @return 结果
     */
    @Override
    public int deleteAstConsumableClaimByIds(Long[] ids) {
        return astConsumableClaimMapper.deleteAstConsumableClaimByIds(ids);
    }

    /**
     * 删除资产-耗材-耗材申领信息
     *
     * @param id 资产-耗材-耗材申领主键
     * @return 结果
     */
    @Override
    public int deleteAstConsumableClaimById(Long id) {
        return astConsumableClaimMapper.deleteAstConsumableClaimById(id);
    }

    @Override
    public List<AstConsumableClaimVo> consumableClaim(Long id) {
        return ConversionUtil.conversionList(astConsumableClaimMapper.consumableClaim(id), AstConsumableClaimVo.class);
    }

    @Override
    public List<AstConsumableClaimPageVo> claimPage(AstConsumableClaimPageDto pageDto) {
        pageDto.setCreateEndTime(DateUtils.toDateLastTime(pageDto.getCreateEndTime()));
        return ConversionUtil.conversionList(astConsumableClaimMapper.claimPage(pageDto), AstConsumableClaimPageVo.class);
    }

    @Override
    public AstConsumableClaimDetailVo claimDetail(Long id) {
        //查询申领详情
        AstConsumableClaim claim = getById(id);
        //查询耗材信息
        List<AstConsumableClaimReserveVo> claimReverse = recordService.claimReserveRecord(id);
        AstConsumableClaimDetailVo detailVo = new AstConsumableClaimDetailVo(claim, claimReverse);
        return ConversionUtil.conversion(detailVo, AstConsumableClaimDetailVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void claim(AstConsumableClaimDto dto) {
        AstConsumableClaim claim = dto.buildClaim();
        //保存申领信息
        save(claim);
        //封装申领资产记录
        List<AstConsumableRecord> recordList = dto.buildClaimRecord(claim);
        recordService.saveBatch(recordList);
        //冻结库存
        consumableService.frostReserve(dto.getReserveList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revoke(Long id) {
        AstConsumableClaim claim = getById(id);
        if (ObjectUtils.isEmpty(claim)) {
            throw new ServiceException("申领记录不存在");
        }
        if (!claim.getAuditState().equals("0")) {
            throw new ServiceException("只有待审核记录,才能撤销");
        }
        //删除申领信息记录
        removeById(id);
        //查询申领记录
        List<AstConsumableRecord> recordList = recordService.lambdaQuery()
                .eq(AstConsumableRecord::getType, "1")
                .eq(AstConsumableRecord::getBusinessId, id)
                .list();
        //删除申领记录
        recordService.lambdaUpdate()
                .eq(AstConsumableRecord::getType, "1")
                .eq(AstConsumableRecord::getBusinessId, id)
                .remove();
        //恢复冻结库存
        recordList.forEach(data -> {
            consumableService.thawReserve(data);
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pass(List<Long> list) {
        list = lambdaQuery()
                .in(AstConsumableClaim::getId,list)
                .eq(AstConsumableClaim::getAuditState,0)
                .list().stream().map(AstConsumableClaim::getId).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(list)){
            //修改审核状态
            lambdaUpdate()
                    .in(AstConsumableClaim::getId, list)
                    .set(AstConsumableClaim::getAuditUser, SecurityUtils.getUserId())
                    .set(AstConsumableClaim::getAuditState, "2")
                    .update();
            //查询申领记录
            List<AstConsumableRecord> recordList = recordService.lambdaQuery()
                    .eq(AstConsumableRecord::getType, "1")
                    .in(AstConsumableRecord::getBusinessId, list)
                    .list();
            //修改库存信息
            recordList.forEach(data -> {
                consumableService.claimPass(data);
            });
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reject(AstRejectedDto dto) {
        //修改审核状态
        lambdaUpdate()
                .eq(AstConsumableClaim::getId, dto.getId())
                .set(AstConsumableClaim::getAuditUser, SecurityUtils.getUserId())
                .set(AstConsumableClaim::getAuditState, "1")
                .set(AstConsumableClaim::getAuditRemark,dto.getRemark())
                .update();
        //查询申领记录
        List<AstConsumableRecord> recordList = recordService.lambdaQuery()
                .eq(AstConsumableRecord::getType, "1")
                .eq(AstConsumableRecord::getBusinessId, dto.getId())
                .list();
        //修改库存信息
        recordList.forEach(data -> {
            consumableService.thawReserve(data);
        });
    }
}
